import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  _buildPropertyItem() {
    List<Widget> list = List();
    for (int i = 0; i < 8; i++) {
      list.add(PropertyItem(
        text: '$i',
        url: "images/items.png",
      ));
    }
    return list;
  }

  _buildPropertyBigItem() {
    List<Widget> list = List();
    for (int i = 0; i < 2; i++) {
      list.add(PropertyItem(
        text: '$i',
        url: "images/items.png",
      ));
    }
    return list;
  }

  _buildFastItem() {
    List<Widget> list = List();
    for (int i = 0; i < 5; i++) {
      list.add(FastItem(
        text: '$i',
        url: "images/items.png",
      ));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('智慧橙',style: TextStyle(color: Colors.black),),
      ),
      body: Column(
        children: <Widget>[
          // banner
          SizedBox(
            height: 130,
            child: Swiper(
              itemBuilder: (BuildContext context, int index) {
                return Image.asset(
                  "images/items.png",
                  fit: BoxFit.fill,
                );
              },
              itemCount: 1,
              pagination: SwiperPagination(
                  builder: DotSwiperPaginationBuilder(
                size: 7,
                activeSize: 7,
                color: Colors.grey,
                activeColor: Colors.white,
              )),
              autoplay: true,
              scrollDirection: Axis.horizontal,
//                control: SwiperControl(),
            ),
          ),
          // 快捷菜单
          SizedBox(
            height: 100,
            child: ListView(
              scrollDirection: Axis.horizontal,
              children: _buildFastItem(),
            ),
          ),
          //物业公告
          PropertyMsg(),
          //物业 item1
          Container(
            color: Colors.grey,
            child: GridView.count(
              // 禁止滚动
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              childAspectRatio: 3 / 1,
              crossAxisCount: 2,
              padding: const EdgeInsets.all(0),
              mainAxisSpacing: 0,
              crossAxisSpacing: 1,
              children: _buildPropertyBigItem(),
            ),
          ),
          //物业 item2
          Container(
            color: Colors.grey,
            child: GridView.count(
              // 禁止滚动
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              childAspectRatio: 2 / 3,
              crossAxisCount: 4,
              padding: const EdgeInsets.all(0),
              mainAxisSpacing: 1,
              crossAxisSpacing: 1,
              children: _buildPropertyItem(),
            ),
          ),
        ],
      ),
    );
  }
}

class FastItem extends StatelessWidget {
  final onTap;
  final text;
  final url;

  const FastItem({Key key, this.onTap, this.text, this.url}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: onTap,
        child: SizedBox(
          width: 100,
          height: 100,
          child: Column(
            children: <Widget>[
              Image.asset(
                url,
                fit: BoxFit.fill,
                width: 50,
                height: 50,
              ),
              Text(
                text,
                style: TextStyle(fontSize: 15, color: Colors.black),
              ),
            ],
          ),
        ));
  }
}

class PropertyMsg extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: <Widget>[
        Image.asset(
          'images/w_notice.png',
          fit: BoxFit.fill,
        ),
        Container(
          width: 5,
          height: 80,
          color: Colors.black,
        ),
        Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Column(
              children: <Widget>[
                Text('尊敬的业主你好'),
                Text('尊敬的业主你好'),
              ],
            ),
            Image.asset('images/ic_corner_right.png')
          ],
        )
      ],
    );
  }
}

class PropertyItem extends StatelessWidget {
  final onTap;
  final text;
  final url;

  const PropertyItem({Key key, this.onTap, this.text, this.url})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: GestureDetector(
          onTap: onTap,
          child: Stack(
            children: <Widget>[
              Column(
                children: <Widget>[
                  Image.asset(
                    url,
                    fit: BoxFit.fill,
                    width: 50,
                    height: 50,
                  ),
                  Text(
                    text,
                    style: TextStyle(fontSize: 15, color: Colors.black),
                  ),
                ],
              ),
              Positioned(
                  right: 1.0,
                  top: 1.0,
                  child: Image.asset(
                    'images/w_more.png',
                    fit: BoxFit.fill,
                  )),
            ],
          )),
    );
  }
}

class PropertyItemBig extends StatelessWidget {
  final onTap;
  final text;
  final url;

  const PropertyItemBig({Key key, this.onTap, this.text, this.url})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: GestureDetector(
        onTap: onTap,
        child: Column(
          children: <Widget>[
            Image.asset(
              url,
              fit: BoxFit.fill,
              width: 50,
              height: 50,
            ),
            Text(
              text,
              style: TextStyle(fontSize: 15, color: Colors.black),
            ),
          ],
        ),
      ),
    );
  }
}
